package cn.tangwei.pet.controller;

import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.basic.utils.LoginContext;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.pet.domain.Pet;
import cn.tangwei.pet.query.PetQuery;
import cn.tangwei.pet.service.IPetService;
import org.springframework.aop.aspectj.AbstractAspectJAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query, HttpServletRequest request){
        return petService.queryPage(query);
    }

    /**
     * 商品上架接口
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxRusult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 商品下架接口
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxRusult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    @GetMapping("/{id}")
    public Pet queryById(@PathVariable("id") Long id){
        return petService.queryById(id);
    }

    /**
     * 购买宠物接口
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/adopt/{id}")
    public AjaxRusult adoptPet(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            petService.adoptPet(id, LoginContext.currentLoginInfo(request).getId());
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("系统繁忙，稍后重试！");
        }
    }

}
